package com.example;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.Collection;
import java.io.PrintWriter;

public class ListBookServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        Collection<Book> books = BookDB.getAllBooks();

        out.println("<html><body>");
        out.println("<h1>书籍列表</h1>");
        for (Book book : books) {
            String url = "PurchaseServlet?id=" + book.getId();
            out.println(book.getName() + " <a href='" + url + "'>点击购买</a><br>");
        }
        out.println("</body></html>");
    }
}